var app = getApp(), util = require("../../utils/util.js");

Page({
    data: {
        VerifyCode: "立即验证",
        bdsjhtext: "验证微信手机号",
        isyz: !0,
        isbd: !1,
        isdx: !0,
        fsyzm: !1
    },
    bindDateChange: function(e) {
        this.setData({
            start: e.detail.value
        });
    },
    hqsjh: function(e) {
        console.log(e.detail.value), this.setData({
            sjh: e.detail.value
        }), "" != e.detail.value ? this.setData({
            isyz: !1
        }) : this.setData({
            isyz: !0
        });
    },
    setVerify: function() {
        var e = util.getRandomNum();
        this.setData({
            yzm: e
        });
        var t = this.data.sjh;
        console.log(t), console.log(e);
        var a = 60, o = this;
        if (!/^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/.test(t) || 11 != t.length) return wx.showToast({
            title: "手机号错误",
            icon: "loading",
            duration: 1e3
        }), !1;
        var n = setInterval(function() {
            --a > 0 ? o.setData({
                VerifyCode: a + " 秒",
                isyz: !0,
                fsyzm: !0
            }) : (o.setData({
                VerifyCode: "立即验证",
                isyz: !1,
                fsyzm: !1
            }), clearInterval(n));
        }, 1e3);
        app.util.request({
            url: "entry/wxapp/sms",
            cachetime: "0",
            headers: {
                "Content-Type": "application/json"
            },
            data: {
                tel: t,
                code: e
            },
            success: function(e) {
                console.log("111111111"), console.log(e), "操作成功" == e.data.reason && wx.showToast({
                    title: "发送成功",
                    icon: "success",
                    duration: 1e3
                });
            },
            fail: function(e) {
                console.log("error res="), console.log(e.data);
            }
        });
    },
    dw: function() {
        var e = this;
        wx.chooseLocation({
            success: function(t) {
                console.log(t), e.setData({
                    weizhi: t.address,
                    jwd: t.latitude + "," + t.longitude
                });
            },
            fail: function() {
                wx.showModal({
                    title: "警告",
                    content: "您点击了拒绝地理位置授权,无法正常使用功能，点击确定重新获取授权。",
                    showCancel: !1,
                    success: function(t) {
                        t.confirm && wx.openSetting({
                            success: function(t) {
                                t.authSetting["scope.userLocation"], e.dw();
                            },
                            fail: function(e) {}
                        });
                    }
                });
            }
        });
    },
    getPhoneNumber: function(e) {
        var t = this;
        console.log(e), console.log(e.detail.iv), console.log(e.detail.encryptedData), "getPhoneNumber:fail user deny" == e.detail.errMsg ? wx.showModal({
            title: "提示",
            showCancel: !1,
            content: "您未授权获取您的手机号",
            success: function(e) {}
        }) : app.util.request({
            url: "entry/wxapp/Jiemi",
            cachetime: "0",
            data: {
                sessionKey: getApp().getSK,
                data: e.detail.encryptedData,
                iv: e.detail.iv
            },
            success: function(e) {
                console.log("解密后的数据", e), null != e.data.phoneNumber && t.setData({
                    sjh: e.data.phoneNumber,
                    isbd: !0,
                    bdsjhtext: "验证成功"
                });
            }
        });
    },
    formSubmit: function(e) {
        console.log("form发生了submit事件，携带数据为：", e);
        var t = this, a = getApp().getOpenId, o = e.detail.formId, n = wx.getStorageSync("UserData").id;
        console.log(n), console.log("form发生了submit事件，携带数据为：", e.detail.value);
        var s = this.data.yzm;
        console.log("随机生成的验证码", s);
        var i = e.detail.value.xm, l = e.detail.value.sjh, c = e.detail.value.yanzm, r = e.detail.value.sr, d = e.detail.value.xxdz, u = this.data.xtxx;
        console.log(i, l, c, r, d, u, a);
        var g = "", p = !0;
        if ("" == i) g = "请填写姓名！"; else if ("" == l) g = "请填写手机号！"; else if (/^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/.test(l) && 11 == l.length) if ("" == c && t.data.isdx) g = "请填写您收到的验证码！"; else if (c != s && t.data.isdx) g = "验证码不正确！"; else {
            p = !1;
            var t = this, f = getCurrentPages();
            if (console.log(f), console.log(Number(u.opencard)), Number(u.opencard) <= 0) console.log("免费"), 
            app.util.request({
                url: "entry/wxapp/AddVip",
                cachetime: "0",
                data: {
                    user_id: n,
                    name: i,
                    tel: l,
                    birthday: r,
                    address: d
                },
                success: function(e) {
                    console.log(e.data), 2 != e.data && (wx.showModal({
                        title: "恭喜成为会员",
                        content: "开卡成功"
                    }), app.util.request({
                        url: "entry/wxapp/Message2",
                        cachetime: "0",
                        data: {
                            openid: a,
                            form_id: o,
                            code: e.data.vip_code,
                            level_name: e.data.level_name,
                            name: e.data.name,
                            tel: e.data.tel
                        },
                        success: function(e) {
                            console.log("msg", e);
                        }
                    }), f.length > 1 && f[f.length - 2].changeData(), setTimeout(function() {
                        wx.navigateBack({});
                    }, 1e3)), 2 == e.data && wx.showToast({
                        title: "提交失败请重试",
                        icon: "loading",
                        duration: 1e3
                    });
                }
            }); else {
                var h = Number(u.opencard);
                console.log(h), app.util.request({
                    url: "entry/wxapp/AddCzOrder",
                    cachetime: "0",
                    data: {
                        user_id: n,
                        name: i,
                        tel: l,
                        birthday: r,
                        address: d,
                        money: h,
                        form_id: o
                    },
                    success: function(e) {
                        console.log(e), "下单失败" != e.data && app.util.request({
                            url: "entry/wxapp/pay2",
                            cachetime: "0",
                            data: {
                                openid: a,
                                money: h,
                                order_id: e.data
                            },
                            success: function(e) {
                                console.log(e), wx.requestPayment({
                                    timeStamp: e.data.timeStamp,
                                    nonceStr: e.data.nonceStr,
                                    package: e.data.package,
                                    signType: e.data.signType,
                                    paySign: e.data.paySign,
                                    success: function(e) {
                                        console.log(e), wx.showModal({
                                            title: "恭喜成为会员",
                                            content: "开卡成功"
                                        }), f.length > 1 && f[f.length - 2].changeData(), setTimeout(function() {
                                            wx.navigateBack({});
                                        }, 1e3);
                                    },
                                    complete: function(e) {
                                        "requestPayment:fail cancel" == e.errMsg && wx.showToast({
                                            title: "取消支付",
                                            icon: "loading",
                                            duration: 1e3
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            }
        } else g = "手机号错误！";
        1 == p && wx.showModal({
            title: "提示",
            content: g
        });
    },
    onLoad: function(e) {
        var t = this;
        app.getUserInfo(function(e) {
            console.log(app.globalData), t.setData({
                userInfo: e
            });
            var a = wx.getStorageSync("UserData").id;
            console.log(a);
        });
        var a = util.formatTime(new Date()).substring(0, 10).replace(/\//g, "-");
        console.log(a.toString()), this.setData({
            date: a,
            start: a
        }), app.util.request({
            url: "entry/wxapp/system",
            cachetime: "0",
            success: function(e) {
                console.log(e.data), "2" == e.data.is_sms && t.setData({
                    isdx: !1
                }), t.setData({
                    xtxx: e.data
                }), wx.setStorageSync("xtxx", e.data), wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: e.data.link_color
                }), wx.setNavigationBarTitle({
                    title: e.data.link_name + "注册"
                });
            }
        }), app.util.request({
            url: "entry/wxapp/url",
            cachetime: "0",
            success: function(e) {
                console.log(e.data), t.setData({
                    url: e.data
                }), getApp().imgurl = e.data;
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {}
});